%PC - Matlab program for controlling a remote controlled car using voice commands
function pc()

%Threshold values for specific commands
GO_T = 300; 		%370
BACK_T = 700; 		%850; %650
STOP_T = 80; 
RIGHT_T = 145; 	%270
LEFT_T = 20;

%some constants
GO = 1;
BACK = -1;
STOP = 0;
RIGHT = 2;
LEFT = -2;
STRAIGHT = 0;
currentFB = 0;
currentRL = 0;
Fs = 11025;		%sampling rate
r = 100;				%counter
%punta0;				%stop as initial condition
for  s = 1:r
   pause;			%wait for user
	stream = wavrecord(1*Fs,Fs,'double');	%get command from user
   
   wavwrite(stream,Fs,'stream.wav');		%write it to file
   wavchop('stream.wav', 'stream.wav');	%get rid of unwanted data 
                                 %(delete parts 15% below the average data)
   [a,b,c]=jfilt('stream.wav');			%make sense out of the data
   fprintf('%d\t%s\t%s\n',a,b,c);		%print result
   
         if (a > BACK_T)				%control the pc
            if (currentRL == LEFT & currentFB == STOP)
               %punta6;
               currentRL = LEFT;
               currentFB = BACK;
            elseif (currentRL == RIGHT & currentFB == STOP)
               %punta10;
               currentRL = RIGHT;
               currentFB = BACK;
            else 
               %punta2;
               currentRL = STRAIGHT;
               currentFB = BACK;
            end               
         elseif(a > GO_T)
            if (currentRL == LEFT & currentFB == STOP)
               %punta5;
               currentRL = LEFT;
               currentFB = GO;
            elseif (currentRL == RIGHT & currentFB == STOP)
               %punta9;
               currentRL = RIGHT;
               currentFB = GO;
            else 
               %punta1;
               currentRL = STRAIGHT;
               currentFB = GO;
            end   
			elseif (a > RIGHT_T)
            if (currentFB == GO)
               %punta9;
               currentFB = GO;
               currentRL = RIGHT;
            elseif (currentFB == BACK)               
               %punta10;
               currentFB = BACK;
               currentRL = RIGHT;
            else
               %punta8;
               currentFB = STOP;
               currentRL = RIGHT;
            end
                    
			elseif (a > STOP_T)
            if (currentFB == GO)
               %punta2;
            elseif (currentFB == BACK)
               %punta1;
            end
            pause(1);
            %punta0;
            currentRL = STRAIGHT;
            currentFB = STOP;
         elseif (a > LEFT_T)
            if (currentFB == GO)
               %punta5;
               currentFB = GO;
               currentRL = LEFT;
            elseif (currentFB == BACK)               
               %punta6;
               currentFB = BACK;
               currentRL = LEFT;
            else
               %punta4;
               currentFB = STOP;
               currentRL = LEFT;
            end	   
         else 
            %punta0;
            currentFB = STOP;
            currentRL = STRAIGHT;
         end
   s = s + 1;
end